package myarraylist;

import java.util.Arrays;

public class MyArrayList implements IList{
    //用数组表示顺序表
    public int[] elem;
    //usedSize表示当前可以存放元素的下标
    public int usedSize;
    //设置一个常量指定数组长度
    public static final int DEFAULT_SIZE=10;
    public MyArrayList(){
        this.elem=new int[DEFAULT_SIZE];
    }
    //可以自定义数组大小的构造方法
    public MyArrayList(int val){
        this.elem=new int[val];
    }

    //判断数组是否为空
    public boolean isEmpty(){
        return usedSize==0;
    }
    //判断数组是否已经满了
    public boolean isFull(){
        return this.usedSize==elem.length;
    }
    //对于数组满了想要继续存放元素进行扩容处理
    public void Expansion(){
        if (isFull()){
            elem= Arrays.copyOf(elem,elem.length*2);
        }
    }
    @Override
    public void add(int data) {
        Expansion();
        elem[usedSize]=data;
        usedSize++;
    }

    @Override
    public void add(int pos, int data) throws isLeagl{
        if (pos<0||pos>usedSize){
            throw new isLeagl("pos位置异常");
        }
        for (int i = usedSize-1; i >pos; i--) {
            elem[i+1]=elem[i];
        }
        elem[pos]=data;
        usedSize++;
    }

    @Override
    public boolean contaions(int toFind) {
        if (usedSize==0){
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            if (elem[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if (size()==0){
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            if (elem[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public void remove(int toRemove) {

        int index=indexOf(toRemove);
        if (index==-1){
            System.out.println("没有找到数字");
        }
        for (int i = index; i <usedSize-1 ; i++) {
            elem[i]=elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int get(int pos) {
        return 0;
    }

    @Override
    public void set(int pos, int data) {

    }

    @Override
    public void clear() {
        this.usedSize=0;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }
    public ListNode mid(){
        ListNode fast=head;
        ListNode solw=head;

        while (fast!=null||fast.next!=null){
            fast=fast.next.next;
            solw=solw.next;
        }
        return solw;
    }
    public ListNode search(){
         if (k<0||k>size()){
             return null;
         }
        ListNode fast=head;
        ListNode solw=head;
        while (k-1!=0){
            fast=fast.next;
            k--;
        }
        while (fast.next!=null){
            solw=solw.next;
            fast=fast.next;
        }
    }

}
